package com.queclink.ycm.entity;

import com.alibaba.fastjson.annotation.JSONField;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2021-12-24 15:21:00
 */
public abstract class FrameType {

    protected String REGEX;
    protected String MessageType;
    protected String strData;
    @JSONField(
            name = "OffsetAddress"
    )
    private Integer OffsetAddress;
    @JSONField(
            name = "RawDataLength"
    )
    private Integer RawDataLength;

    public FrameType() {
    }

    public Integer getOffsetAddress() {
        return this.OffsetAddress;
    }

    public void setOffsetAddress(Integer offsetAddress) {
        this.OffsetAddress = offsetAddress;
    }

    public Integer getRawDataLength() {
        return this.RawDataLength;
    }

    public void setRawDataLength(Integer rawDataLength) {
        this.RawDataLength = rawDataLength;
    }

    @JSONField(
            name = "MessageType"
    )
    public String getMessageType() {
        return this.MessageType;
    }

    public void setMessageType(String messageType) {
        this.MessageType = messageType;
    }

    public Matcher matcher(byte[] buf) throws Exception {
        String str = new String(buf, "UTF-8");
        Pattern p = Pattern.compile(this.REGEX);
        Matcher m = p.matcher(str);
        return m;
    }

    public boolean validateMatcher(byte[] buf) throws Exception {
        Matcher m = this.matcher(buf);
        if (m.find()) {
            this.strData = m.group(0);
            this.parser();
            return true;
        } else {
            return false;
        }
    }

    public abstract void parser() throws Exception;
}
